﻿using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GanagroLiteData;
using Csla.Validation;

namespace GanagroLiteBO
{
    class GanagroLiteBOTemplate : IEditableObject, INotifyPropertyChanged, IDataErrorInfo
    {
        private short _tipoOperacion;

        #region Properties
        public TiposOperacion TipoOperacion
        {
            get { return (TiposOperacion)_tipoOperacion; }
            set { _tipoOperacion = (short)value; }
        }

        DateTime _fecha;
        public DateTime Fecha
        {
            get { return _fecha; }
            set
            {
                if (_fecha != value)
                {
                    _fecha = value.Date;
                    FirePropertyChangedNotification("Fecha");
                }
            }
        }

        #endregion

        #region Constructor

        public GanagroLiteBOTemplate(TiposOperacion tOp)
        {
            _tipoOperacion = (short)tOp;
            AddBusinessRules();
        }


        #endregion

        #region Methods
        public void Guardar()
        {

        }

        #endregion Methods

        #region Validaciones

        private ValidationRules _validationRules;
        public ValidationRules ValidationRules
        {
            get
            {
                if (_validationRules == null)
                    _validationRules = new ValidationRules(this);
                return _validationRules;
            }
        }

        public bool Isvalid()
        {
            ValidationRules.CheckRules();
            return ValidationRules.GetBrokenRules().ErrorCount == 0;
        }

        public string Warnings()
        {
            {
                return ValidationRules.GetBrokenRules().ToString(Csla.Validation.RuleSeverity.Warning);
            }
        }


        private void AddBusinessRules()
        {
            try
            {
              // ValidationRules.AddRule(new Csla.Validation.RuleHandler<GanagroLiteBOTemplate,RuleArgs>(), "Fecha");
            }
            catch (Exception ex)
            {
                throw (new Exception("AddBusinessRules: " + ex.Message, ex));
            }

        }

        #endregion

        #region INotifyPropertyChanged Implementation
        protected void FirePropertyChangedNotification(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
            ValidationRules.CheckRules(propName);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        #region IDataErrorInfo Members


        [Browsable(false)]
        public string Error
        {
            get
            {
                if (!ValidationRules.IsValid)
                {
                    return ValidationRules.GetBrokenRules().First().Description;
                }
                else
                { return String.Empty; }
            }
        }

        [Browsable(false)]
        public string this[string columnName]
        {
            get
            {
                string result = string.Empty;
                if (!ValidationRules.IsValid)
                {
                    BrokenRule rule = ValidationRules.GetBrokenRules().GetFirstBrokenRule(columnName);
                    if (rule != null)
                        result = rule.Description;
                }

                return result;
            }
        }
        #endregion

        #region IEditableObject Members

        public void BeginEdit()
        {
        }

        public void CancelEdit()
        {
        }

        public void EndEdit()
        {
        }

        #endregion

    }
}
